using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using System.IO;

//using System.Diagnostics;
//using System.Threading;

namespace HubLog
{
    public class LoadRemoteSources
    {
        private String configName;
        private RemoteSource[] rs = null;
        private XPathDocument xdoc;
        public LoadRemoteSources()
        {
            //
            configName = Path.GetFileName(Application.ExecutablePath) + ".Xml";
            xdoc = null;
            //
            //
        }

        public string ConfigFileName { get { return this.configName; } }

        public RemoteSource[] Initialize(){
            //
            xdoc = null;
            xdoc = new XPathDocument(new XmlTextReader(configName), XmlSpace.Preserve);
            XPathNavigator xpn = xdoc.CreateNavigator();
            XPathNodeIterator xni = null;
            xni = xpn.Select("/Config/Instances/Instance");
            //Console.WriteLine("xni.Count-->{0}", xni.Count);
            System.Diagnostics.Debug.WriteLine(string.Format("xni.Count-->{0}", xni.Count));
            if (0 == xni.Count)
            {
                //rs = null;
                return null;
            }
            rs = new RemoteSource[xni.Count];
            int i = 0;
            while (xni.MoveNext())
            {
                XPathNavigator xnav2 = xni.Current.Clone();
                //Console.WriteLine("[{0}]", xnav2.Value);
                //System.Diagnostics.Debug.WriteLine(string.Format("[{0}]", xnav2.Value));
                String s;
                s=xnav2.SelectSingleNode("InstanceName").ToString();
                s=xnav2.SelectSingleNode("Host").ToString();
                s=xnav2.SelectSingleNode("Port").ToString();
                s=xnav2.SelectSingleNode("UserName").ToString();
                s=xnav2.SelectSingleNode("Password").ToString();
                xnav2.SelectSingleNode("InstanceColor").ToString();
                xnav2.SelectSingleNode("VeryImportant").ToString();
                rs[i] = new RemoteSource();
                rs[i].Set(xnav2.SelectSingleNode("InstanceName").ToString(), xnav2.SelectSingleNode("Host").ToString(), xnav2.SelectSingleNode("Port").ToString(), xnav2.SelectSingleNode("UserName").ToString(), xnav2.SelectSingleNode("Password").ToString(), xnav2.SelectSingleNode("InstanceColor").ToString(), xnav2.SelectSingleNode("VeryImportant").ValueAsBoolean, xnav2.SelectSingleNode("UserFunc").ToString());
                //
                string rx = xnav2.SelectSingleNode("Regex").ToString().Trim();
                if (string.Empty != rx) rs[i].RegularExpression = new Regex(rx, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                //
                i++;
            }
            //
            return rs;
        }
    }//class
}//namespace
